﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class ChiTIetHoaDonDAO
    {
        
        public DataTable LayTable()
        {
            String sql = "SELECT ct.MaChiTietHoaDon,MaHoaDon,ct.SoLuong as slban,ct.MaSach,TenSach,s.SoLuong AS slton,DonGia ,'XÓA' as xoa from ChiTietHoaDon ct,Sach s where ct.MaSach =s.MaSach";
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTableTheoHoaDon(int maHD)
        {
            String sql = "SELECT ct.MaChiTietHoaDon,MaHoaDon,ct.SoLuong as slban,ct.MaSach,TenSach,s.SoLuong AS slton,DonGia ,'XÓA' as xoa from ChiTietHoaDon ct,Sach s where ct.MaSach =s.MaSach AND MaHoaDon = " + maHD;
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public void them(ChiTietHoaDonDTO chitiet)
        {
            String sql = "Insert into ChiTietHoaDon(MaHoaDon,MaSach,SoLuong) values(" + chitiet.MaHoaDon + "," + chitiet.MaSach + "," + chitiet.SoLuong + ")";
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        public void Xoa(ChiTietHoaDonDTO chitiet)
        {
            String sql = "DELETE from ChiTietHoaDon  where MaChiTietHoaDon =" + chitiet.MaChiTietHoaDon;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        public void delete(ChiTietHoaDonDTO chitiet)
        {
            String sql = "DELETE from ChiTietHoaDon  where MaChiTietHoaDon =" + chitiet.MaChiTietHoaDon;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
       
    }
}
